package com.example.demo.controller.wx;

import com.example.demo.service.wx.impl.WxServiceImpl;
import com.example.demo.util.CommWxUrl;
import com.example.demo.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 鉴权块信息
 * @author: liyuan
 * @data 2020-11-23 16:30
 */
@RestController
@RequestMapping("/")
public class JsAuthTicketController {

    private Logger logger = LoggerFactory.getLogger(JsAuthTicketController.class);
    @Autowired
    private WxServiceImpl wxService;

    /***
     * 需要缓存一下比较好
     * @return
     */
    @RequestMapping("genTicket")
    public ResultVO genTicket() {
        String ticket = wxService.getTicket();
        if (StringUtils.isEmpty(ticket)) {
            return ResultVO.fail();
        }
        return new ResultVO(1000, ticket);
    }

    @RequestMapping("genSignature")
    public ResultVO genSignature(@RequestParam("url") String url) {
        if (StringUtils.isEmpty(url)) {
            return ResultVO.fail();
        }
        String ticket = wxService.getTicket();
        if (StringUtils.isEmpty(ticket)) {
            return ResultVO.fail();
        }
        String signature = CommWxUrl.genJSSignature(ticket, url);
        return new ResultVO(1000, signature);
    }

}
